package model;

@SuppressWarnings("unchecked")
public class SelectionSort extends Sorter {

	public SelectionSort() {
	    super();
	    statistics = new Statistics();
	}

	@Override
	public void sort(Comparable[] x) {
	    long beforeTime = System.nanoTime();
	    
	    for (int i=0; i<x.length-1; i++) {
	        int minIndex = i;      // Index of smallest remaining value.
	        for (int j=i+1; j<x.length; j++) {
	            //Check
	            statistics.incrementChecks();
	            
	            if (x[minIndex] .compareTo( x[j] ) > 0) {
	                minIndex = j;  // Remember index of new minimum
	            }
	        }
	        if (minIndex != i) { 
	            //...  Exchange current element with smallest remaining.
	            //Swap
	            //---------------
	            statistics.incrementSwaps();
	            if(simulation)
	            PushSwapToQueue(i, minIndex);
	            //---------------
	            Integer temp = (Integer) x[i];
	            x[i] = x[minIndex];
	            x[minIndex] = temp;
	        }
	    }
	    
	    long afterTime = System.nanoTime();
	    statistics.setRunTime(afterTime - beforeTime);
	}

	@Override
	public int getComplexity(int arraySize) {
	    return (int) Math.pow(arraySize, 2);
	}

}
